package com.bsit.obu.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.google.gson.Gson;

import java.util.Map;

public class SpMapUtils {
    /**
     * 保存在手机里面的文件名
     */
    public static final String FILE_NAME = "config_data";

    public static void setMapToSharedpreference(Context context, String key, Map<String,String> map ){
        SharedPreferences sha=  context.getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
        SharedPreferences.Editor editor =  sha.edit();
        String json = new Gson().toJson(map);
        editor.putString(key,json);
        editor.commit();
    }

    public static  Map<String,String> getMapToSharedpreference(Context context, String key){
        SharedPreferences sp=  context.getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
        Gson gson = new Gson();
        String json = sp.getString(key, "");
        if(TextUtils.isEmpty(json)){
            return null;
        }
        return  gson.fromJson(json, Map.class);
    }

    public static void removeFromSharedPreference(Context context,String key){
        SharedPreferences sha=  context.getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
        sha.edit().remove(key).commit();
    }
}
